package api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.SneakyThrows;
import model.UserInfo;
import util.AppVar;
import util.DBUtils;
import util.ResultUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @Author: wenjingyuan
 * @Date: 2023/03/20/19:40
 * @Description:登陆
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/json;charset=utf-8");//定义参数
        int data =0;
        String msg="未知错误";

        //1. 得到前端参数，并进行非空校验
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        if(!StrUtil.isBlank(username) &&
        !StrUtil.isBlank(password)){
            //2.查询数据库，验证用户名和密码是否正确
            //2.1拿到连接
            Connection connection= DBUtils.getConnection();
            //2.2拼接sql,拿到执行器
            String sql="select * from userinfo where username=? and password=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,username);
            statement.setString(2, SecureUtil.md5(password));
            //3.执行sql 并得到结果
            ResultSet resultSet=statement.executeQuery();
            if(resultSet.next()){
                data=1;
                //新建一个对象设置对象的信息为用户登录的信息
                UserInfo userInfo=new UserInfo();
                userInfo.setUid(resultSet.getInt("uid"));
                userInfo.setUsername(resultSet.getString("username"));
                userInfo.setPassword(resultSet.getString("password"));
                userInfo.setNickname(resultSet.getString("nickname"));
                userInfo.setImg(resultSet.getString("img"));
                userInfo.setGit(resultSet.getString("git"));
                userInfo.setState(resultSet.getInt("state"));
                //将用户登录的信息保存到session
                //1.得到httpsession对象
                HttpSession session=req.getSession(true);
                //true代表查看有么有回session，没有就创建有就直接使用
                //2.给session设置属性
                if(session!=null){
                    session.setAttribute(AppVar.SESSION_USERINFO_KEY,userInfo);
                }
            }else{
                msg="用户名或者密码错误！";
            }
            //4.关闭资源
            DBUtils.close(resultSet,statement,connection);
        }else{
            msg="非法参数";
        }
        //3.将结果返回给前端
       resp.getWriter().println(JSONUtil.toJsonStr(ResultUtils.toResult(200,data,msg))) ;
    }
}
